package com.shaoshaohuo.app.adapter;

import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Coupon;
import com.shaoshaohuo.app.utils.StringHelper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

public class SelectCouponAdapter<T> extends MyBaseAdapter<Coupon> {

     private int mSelectPosition = -1;
    
    public SelectCouponAdapter(Context ctx, List<Coupon> list, boolean flag) {
        super(ctx, list, flag);
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup group) {
        
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_coupon_list_item, null);
            holder = new ViewHolder();
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_select);
            holder.titleText = (TextView) convertView.findViewById(R.id.textview_money);
            holder.timeText = (TextView) convertView.findViewById(R.id.textview_coupon_name);
            holder.contentText = (TextView) convertView.findViewById(R.id.textview_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        Coupon msg = list.get(pos);
        
        if(mSelectPosition == pos){
             holder.checkBox.setChecked(true);
        }else{
            holder.checkBox.setChecked(false);
        }
        
        holder.titleText.setText(msg.getTotal());
        holder.timeText.setText(msg.getContent());
        holder.contentText.setText(StringHelper.formatDateDay(msg.getEndtime())+"过期");
        
        return convertView;
    }
    
    public void setSelectPosition(int position){
        if(mSelectPosition == position){
            mSelectPosition = -1;
        }else{
            this.mSelectPosition = position;
        }
        notifyDataSetChanged();
    }
    
    public Coupon getSelectItem(){
        if(mSelectPosition == -1){
            return null;
        }
        Coupon coupon = list.get(mSelectPosition);
        return coupon;
    }
    
    static class ViewHolder{
        CheckBox checkBox;
        TextView titleText;
        TextView timeText;
        TextView contentText;
    }
    
}
